﻿
#include "CreateCubeDlg.h"
#include <vtkCubeSource.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreateCubeDlg::CreateCubeDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreateCubeDlg::~CreateCubeDlg()
{
}

bool CreateCubeDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_startPointCoord->getValues(m_startPoint[0], m_startPoint[1], m_startPoint[2]);
        m_lengthEdit->getValues(m_length[0], m_length[1], m_length[2]);

        //create vtk vtkLineSource
        vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
        cubeSource->SetBounds(m_startPoint[0], m_startPoint[0] + m_length[0],
                              m_startPoint[1], m_startPoint[1] + m_length[1],
                              m_startPoint[2], m_startPoint[2] + m_length[2]);
        cubeSource->Update();

        //create vtk vtkPolyDataMapper
        vtkSmartPointer<vtkPolyDataMapper> cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        cubeMapper->SetInputData(cubeSource->GetOutput());
        cubeMapper->StaticOn();//设置静态属性，提高渲染效率(提升40%)


        //create vtk vtkActor
        vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
        cubeActor->SetMapper(cubeMapper);
        cubeActor->GetProperty()->SetColor(QtVtkModle::Global::CubeColor[0], QtVtkModle::Global::CubeColor[1], QtVtkModle::Global::CubeColor[2]);

        pVtkViewWindow->GetRenderer()->AddActor(cubeActor);
        GetActorManagerInstance()->AddActor(cubeActor, PickMode::VOLUME);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreateCubeDlg::initUI()
{
    setWindowTitle("Create Cube");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_startPointCoord = new MyCoordinateEdit(this, "Start Point");
        pDlgLayout->addWidget(m_startPointCoord);

        m_lengthEdit = new MyCoordinateEdit(this, "Length");
        m_lengthEdit->setValues(1, 1, 1);
        pDlgLayout->addWidget(m_lengthEdit);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreateCubeDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create cube!");
                }

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreateCubeDlg::close);
}



